<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>NodeCMS-添加花语</title>
    <link rel="stylesheet" href="/css/layui/css/layui.css">
    <link rel="stylesheet" href="/css/admin/reset.css">
    <link rel="stylesheet" href="/css/admin/admin.css">
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <!-- 公共头部 -->
    <%- include('../common/header') %>
    <!-- 公共头部 -->
    <!-- 导航条 -->
    <%- include('../common/nav') %>
    <!-- 导航条 -->
    <div class="layui-body">
        <div class="container">
            <div class="layui-form" lay-filter="item-info">
                <div class="layui-form-item">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" lay-verify="title" id="title" autocomplete="off" placeholder="请输入花束名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">鲜花种类</label>
                    <div class="layui-input-inline">
                        <select name="type" id="type" lay-filter="type">
                            <option value="">请选择种类</option>
                            <% for(var i=0;i<FlowerList.length;i++){ %>
                            <option value="<%= FlowerList[i].type %>" data-typename="<%= FlowerList[i].name %>"><%= FlowerList[i].name %></option>
                            <% } %>
                        </select>
                    </div>
                    <div class="layui-input-inline">
                        <select name="category" id="category" lay-filter="category">
                            <option value="">请选择鲜花品种</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item" style="position: relative;">
                    <button type="button" id="setThumb" class="layui-btn thimgbox">缩略图上传</button>
                    <blockquote class="layui-elem-quote layui-quote-nm" style="margin-top: 10px;">
                        预览图：
                        <div class="layui-upload-list" id="thumbs-box"></div>
                    </blockquote>
                </div>
                <input type="hidden" id="thumbinput" value="" name="thumb">
                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <input type="button" id="submitBtn" class="layui-btn" value="立即提交">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<input type="hidden" id="qiniuToken" value="<%= qiniuToken %>">
<input type="hidden" id="flowerData" value="<%= JSON.stringify(FlowerList) %>">
<script src="/js/jquery.min.js"></script>
<script src="/css/layui/layui.js"></script>
<script src="/js/wangEditor.min.js"></script>
<script src="/js/plupload.full.min.js"></script>
<script src="/js/qiniu.js"></script>
<script src="/js/addFlowerPic.js"></script>
<script>
    // 初始化七牛上传
    uploadInit();
    layui.use(['element', 'form'], function(){
        var element = layui.element;
        var form = layui.form;
        var upload = layui.upload;
        var flowerData = JSON.parse($('#flowerData').val());
        var flowerType = '';
        var flowerTypeName = '';
        var categoryName = '';
        form.on('select(type)', function(data){
            flowerType = $(data.elem).find("option:selected").val();
            flowerTypeName = $(data.elem).find("option:selected").attr('data-typename');
            var flowerCategory = '';
            var dom = '';
            $.each(flowerData, function(){
                if(this.type == flowerType){
                    flowerCategory = this.category;
                    return false;
                }
            })
            $("#category").empty();
            $.each(flowerCategory, function(){
                dom += '<option value="' + this.id + '" data-cname="' + this.name + '">' + this.name + '</option>';
            });
            $('#category').append(dom);
            form.render('select');
        })
        form.on('select(category)', function(data){
            categoryName = $(data.elem).find("option:selected").attr('data-cname');
        })
        $('#submitBtn').on('click', function(){
            var data = form.val('item-info');
            data.typename = flowerTypeName;
            data.categoryname = categoryName;
            if(!$.trim(data.title)){
                alert('请填写标题');
                return;
            }
            $.ajax({
                url:'/cms/addFlowerPic',
                type:'post',
                data: data,
                success : function(d){
                    if(d.code == '200'){
                        window.location.href = '/admin/flowerPicList';
                    }else{
                        alert('出错了，请重新打开页面');
                    }
                }
            })
        })
    });
</script>
</body>
</html>
